package com.szcinda.fitness.security;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public final class SecurityUtils {

    public static AuthenticatedUser getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null||"anonymous".equals(authentication.getPrincipal())) {
            AuthenticatedUser authenticatedUser = new AuthenticatedUser();
            authenticatedUser.setUsername("system");
            return authenticatedUser;
        }
        UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        return (AuthenticatedUser) token.getPrincipal();
    }
}
